﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAttack : MonoBehaviour {

	public static PlayerAttack instance;
	public Gun heroGun;

	public Transform targetTran;//攻击目标
	public bool isFire = false;//是否攻击状态
	public WeaponInfo weaponInfo;//武器信息
	public int weaponId;//根据ID寻找武器

	private float cdTimer  = 0;
	private float time = 0;

	public List<Collider2D> colliderArr = new List<Collider2D>();//储存所有物体的collider
	public List<Collider2D> colliderTempArr = new List<Collider2D>();//物体移出检测范围的时候过滤colliderArr
	public List<Collider2D> tempArr = new List<Collider2D>();//遍历开始之前过滤colliderArr
	public float[] distancesTemp;//过滤distanceArr
	public float[] hpArrTemp;//过滤hpArr
	public int whichTarget;//攻击目标索引

	public float[] distanceArr;//所有目标距离
	public float[] hpArr;//所有血量
	
	public float minDistance;//最近距离
	public float maxHp;//最高血量
	public float minHp;//最低血量

	private int attackState = 1;//攻击策略
	public bool isCloaking = false;//是否隐身状态

	


	/// <summary>
	/// Awake is called when the script instance is being loaded.
	/// </summary>
	void Awake()
	{
		instance = this;
		
		
	}

	// Use this for initialization
	void Start () {
		/*
		这里设置武器
		 */
		//  weaponId = 4;
		 if(weaponId != 0)
		 {
			SetWeapon(weaponId);
		 }
		 
		 if(weaponInfo != null)
		{			
			cdTimer = weaponInfo.AttackRateTime;
			//  weaponInfo.ShowSelf();
			switch (weaponInfo.BaseType)
			{
				case 1:
					this.GetComponent<CircleCollider2D>().radius = weaponInfo.AttackDistance;
					break;
				case 2:
					break;
				case 3:
					break;
			}
		 }
	}


	
	// Update is called once per frame
	void Update () {
		if(weaponInfo != null )
		{
			switch (weaponInfo.BaseType)
			{

				case 1:
					FindTarget();
					break;
				case 2:
					targetTran = this.transform.parent;
					break;
				case 3:
					targetTran = this.transform.parent;
					break;
			}
			if (weaponInfo.IsReady == false) {
				time += Time.deltaTime;
				if (time >= weaponInfo.AttackRateTime) {
					time = 0;
					weaponInfo.IsReady = true;
					AttackTarget();
				}
			} else 
			{
				AttackTarget();
			}
		}

	}

	// 寻找并设置攻击目标
		// if(weaponId == 0)return;
	private void FindTarget(){
		
		if(colliderArr.Count != 0)
		{
			// 过滤colliderArr空的元素
			for (int j = 0; j < colliderArr.Count; j++)
				{
					if(colliderArr[j] != null && colliderArr[j].tag == "Enemy")
					{
						tempArr.Add(colliderArr[j]);
					}
				}
				colliderArr.Clear();
				for (int k = 0; k < tempArr.Count; k++)
				{
					colliderArr.Add(tempArr[k]);
				}
			tempArr.Clear();
			if(colliderArr.Count == 0)return;
			switch (attackState)
			{
				case 1 :
					SetTargetByMinDistance();
					break;
				case 2 :
					SetTargetByMinHp();
					break;
				case 3 :
					SetTargetByMaxHp();
					break;
				case 4 :
					targetTran = null;
					return;
					// break;
			}
			if(colliderArr[whichTarget] != null)
			{
				targetTran = colliderArr[whichTarget].transform;
			}
		}
		else
		{
			targetTran = null;
		}
	}


	// 最小攻击距离策略
	private void SetTargetByMinDistance()
	{
		distanceArr = new float[colliderArr.Count];
		//得到距离集合 
		for (int i = 0; i < colliderArr.Count; i++)
		{
			if(colliderArr[i] != null && colliderArr[i].tag == "Enemy"){
				distanceArr[i] = Vector3.Distance(this.transform.position, colliderArr[i].transform.position);
			}
		}

		//过滤distanceArr中元素为0的
		distancesTemp = new float[distanceArr.Length];
		for (int j = 0; j < distanceArr.Length; j++)
		{
			if(distanceArr[j] != 0)
			{
				distancesTemp[j] = distanceArr[j];
			}
		}
		for (int k = 0; k < tempArr.Count; k++)
		{
			distanceArr[k] = distancesTemp[k];
		}

		// 遍历得到最小距离
		minDistance = distanceArr[0];
		for (int i = 0; i < distanceArr.Length; i++)
		{
			if(minDistance >= distanceArr[i])
			{
				// if(distanceArr[i] != 0)
				// {
				minDistance = distanceArr[i];
				whichTarget = i;
				// }
			}
		}
	}




	//最小血量攻击策略 
	private void SetTargetByMinHp()
	{
		GetAllHp();
		//遍历得到最小血量
		minHp = hpArr[0];
		for (int i = 0; i < hpArr.Length; i++)
		{
			if(minHp >= hpArr[i])
			{
				// if(distanceArr[i] != 0)
				// {
				minHp = hpArr[i];
				whichTarget = i;
				// }
			}
		}
	}
	


	// 最大血量攻击策略
	private void SetTargetByMaxHp()
	{
		GetAllHp();
		//遍历得到最大血量
		maxHp = hpArr[0];
		for (int i = 0; i < hpArr.Length; i++)
		{
			if(maxHp <= hpArr[i])
			{
				// if(distanceArr[i] != 0)
				// {
				maxHp = hpArr[i];
				whichTarget = i;
				// }
			}
		}
	}

	private void GetAllHp()
	{
		hpArr = new float[colliderArr.Count];
		for (int i = 0; i < colliderArr.Count; i++)
		{
			if(colliderArr[i] != null && colliderArr[i].tag == "Enemy"){
				if(colliderArr[i].gameObject.name == "star(Clone)")
				{
					hpArr[i] = colliderArr[i].gameObject.GetComponent<Star>().GetHp();
				}
				if(colliderArr[i].gameObject.name == "smallEnemyPlane(Clone)")
				{
					hpArr[i] = colliderArr[i].gameObject.GetComponent<SmallEnemyPlane>().GetHp();
				}
				if(colliderArr[i].gameObject.name == "middleEnemyPlane(Clone)")
				{
					hpArr[i] = colliderArr[i].gameObject.GetComponent<MiddleEnemyPlane>().GetHp();
				}
				if(colliderArr[i].gameObject.name == "bigEnemyPlane(Clone)")
				{
					hpArr[i] = colliderArr[i].gameObject.GetComponent<BigEnemyPlane>().GetHp();
				}
			}

		}
		//过滤血量为0的
		hpArrTemp = new float[hpArr.Length];
		for (int j = 0; j < hpArr.Length; j++)
		{
			if(hpArr[j] != 0)
			{
				hpArrTemp[j] = hpArr[j];
			}
		}
		for (int k = 0; k < tempArr.Count; k++)
		{
			hpArr[k] = hpArrTemp[k];
		}
	}


	// 攻击目标
	private void AttackTarget(){
		
		if(targetTran != null && Vector3.Distance(targetTran.transform.position, transform.parent.transform.position) - targetTran.GetComponent<CircleCollider2D>().radius <= weaponInfo.AttackDistance)
		{
//			print("target--->" + targetTran.gameObject.name);
			if(heroGun == null)return;
			heroGun.SetTarget(targetTran.gameObject);
			if(!isFire && !isCloaking)
			{
				isFire = true;
				heroGun.OpenFire(weaponInfo);
			}
			
			
		}
		else
		{
			if(isFire || isCloaking)
			{	
				heroGun.SetTarget(null);
				heroGun.StopFire();
				isFire = false;
			}
		}
	}

	
	void OnTriggerEnter2D(Collider2D collision)
	{
		if(collision.tag == "Enemy"){
			colliderArr.Add(collision);
			colliderTempArr.Add(collision);
		}
	}

	/// <summary>
	/// Sent when another object leaves a trigger collider attached to
	/// this object (2D physics only).
	/// </summary>
	/// <param name="other">The other Collider2D involved in this collision.</param>
	void OnTriggerExit2D(Collider2D collision)
	{
		if(collision.tag == "Enemy")
		{
			colliderArr.Clear();
			for (int i = 0; i < colliderTempArr.Count; i++)
			{
				if(colliderTempArr[i] != collision)
				{
					colliderArr.Add(colliderTempArr[i]);
				}
			}

			colliderTempArr.Clear();
			for (int i = 0; i < colliderArr.Count; i++)
			{
				colliderTempArr.Add(colliderArr[i]);
			}
		}
	}


	/// <summary>
	/// Sent each frame where another object is within a trigger collider
	/// attached to this object (2D physics only).
	/// </summary>
	/// <param name="other">The other Collider2D involved in this collision.</param>
	// void OnTriggerStay2D(Collider2D collision)
	// {
	// 	if(collision.tag == "Enemy")
	// 	{
	// 		colliderArr.Clear();
	// 		for (int i = 0; i < colliderTempArr.Count; i++)
	// 		{
	// 			if(colliderTempArr[i] != collision)
	// 			{
	// 				colliderArr.Add(colliderTempArr[i]);
	// 			}
	// 		}

	// 		colliderTempArr.Clear();
	// 		for (int i = 0; i < colliderArr.Count; i++)
	// 		{
	// 			colliderTempArr.Add(colliderArr[i]);
	// 		}
	// 	}
	// }

	



	private void SetWeapon(int weaponId){
		this.weaponInfo = DataLists.instance.FindWeaponInfoById(weaponId);
//		 this.weaponInfo.ShowSelf();
	}

	public void SetAttackState(int attackState)
	{
		this.attackState = attackState;
	}
}
